#include "TS7260IO.h"
//MEGA BUG KILLED!  variable start must be of char for the purpose of IO
// array access by addition of byte-size addresses
//otherwise addressing was off by 4 (int-size)!

TS7260IO::TS7260IO()
{
    perror("TS7260IO::TS7260IO()");                
   
    fd = open("/dev/mem", O_RDWR|O_SYNC);
    if (fd == -1)
    {
       perror("\t openmem failure");
       return;    
    }

    printf("\tpagesize() %d\n", getpagesize());
    
    start = (volatile unsigned char*) mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x80840000);
    //assert(&start != MAP_FAILED);
    
    if (start == MAP_FAILED) 
    {
       perror("\tmmap failure");
       return;
    }
    
    PC104_base = (volatile unsigned char*) mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x11E00000);
    
 //   PC104_8 = &PC104_base[0];
 
    //REF ONLY////////////////////////
            // start2 = mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x12C0000);
            // assert(&start2 != MAP_FAILED);
            
            //	syscon = (unsigned int *)mmap(0, getpagesize(),	PROT_READ|PROT_WRITE, MAP_SHARED, fd, SYSCONBASE);
            
               //Disable DMA on GPIO
            //	*(syscon + SFTWR_LCK) = 0xAA;
            //	*(syscon + DEVICE_CFG)  = 0xE8140d00;
            /*
            PBDR = (unsigned int *)(start + 0x04);     // port b data - DIO1 Header
            PBDDR = (unsigned int *)(start + 0x14);    // port b direction register
            
            PEDR = (unsigned int *)(start + 0x20);     // port e data
            PEDDR = (unsigned int *)(start + 0x24);    // port e direction register
            */
      /////////////////////      
    /*
    NONwrapped IO
    PADR  = &start[0x00];    // port a data - LCD Header
    PADDR = &start[0x10];    // port a direction register
    
    PBDR  = &start[0x04];    // port b data - DIO1 Header bits 0-7
    PBDDR = &start[0x14];    // port b direction register
    
    PCDR  = &start[0x08];    //DIO1 Header pin 4 (portc bit 0/ADC0 only) "r w m cycles only???"
    PCDDR = &start[0x18];
    
    PEDR  = &start[0x20];    // port e data - on board LEDS only(bit 0 grn LED, bit 1 red LED; active high)
    PEDDR = &start[0x24];    // port e direction register    
    
    PFDR  = &start[0x30];    // DIO1 Header pin 8(DIO_8:  port F bit 1 only)
    PFDDR = &start[0x34];
    
    PHDR  = &start[0x40];    //LCD_EN, LCD_RS, LCD_WR (bits 3 to 5 only) protect other bits!
    PHDDR = &start[0x44];    //
    */
//    printf("\tblahright %x, %x\n", (unsigned int *)(start+0x04), *(unsigned int *)(start+0x04));
  //  printf("\tblahright0x14 %x, %x\n", (unsigned int *)(start+0x14), *(unsigned int *)(start+0x14));

//wrapped IO
    PA  = new IO(&start[0x00], &start[0x10]);    // port a data - LCD Header
    //PADDR = &start[0x10];    // port a direction register
    
    PB  = new IO(&start[0x04], &start[0x14]);    // port b data - DIO1 Header bits 0-7
    //PBDDR = &start[0x14];    // port b direction register
    
    PC  = new IO(&start[0x08], &start[0x18]);    //DIO1 Header pin 4 (portc bit 0/ADC0 only) "r w m cycles only???"
   // PCDDR = &start[0x18];
    
    PE  = new IO(&start[0x20], &start[0x24]);    // port e data - on board LEDS only(bit 0 grn LED, bit 1 red LED; active high)
   // PEDDR = &start[0x24];    // port e direction register    
    
    PF  = new IO(&start[0x30], &start[0x34]);    // DIO1 Header pin 8(DIO_8:  port F bit 1 only)
   // PFDDR = &start[0x34];
    
    PH  = new IO(&start[0x40], &start[0x44]);    //LCD_EN, LCD_RS, LCD_WR (bits 3 to 5 only) protect other bits!
   // PHDDR = &start[0x44];
    //GPIOBDB = (unsigned int *)(start + 0xC4);  // debounce on port b
    
    // *PBDDR = 0xf0;			      // upper nibble output, lower nibble input
   // *PBDDR = 0x00;		
    
    return;
}


TS7260IO::~TS7260IO()
{
   perror("~TS7260IO()");
   delete (PA); 
   delete (PB);
   delete (PC);
   delete (PE);
   delete (PF);
   delete (PH);
 
   return;
}

void TS7260IO::reportAllIO(unsigned char io[12])
{
     unsigned char addresses[12] = {0x00, 0x10, 0x04, 0x14, 0x08, 0x18, 0x20, 0x24, 0x30, 0x34, 0x40, 0x44};
     
     for(int i = 0; i < 12; i++)
     {
             io[i] = start[addresses[i]];
     }
     return;      
}

void TS7260IO::printAllIO()
{
     unsigned char addresses[12] = {0x00, 0x10, 0x04, 0x14, 0x08, 0x18, 0x20, 0x24, 0x30, 0x34, 0x40, 0x44};
    
     printf("\tPrintfAllIO(): "); 
     for(int i = 0; i < 12; i++)
     {
             printf(" %02X", start[addresses[i]]);
     }
     
     printf("\n");
     
     return;      
}

